package likou;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/08/21 08:39
 */
public class _213 {
    public static void main(String[] args) {
        System.out.println(rob(new int[]{1, 2, 3, 1}));
    }

    public static int rob(int[] nums) {
        if (nums.length == 1) return nums[0];
        if (nums.length == 0) return 0;
        return Math.max(getRob(nums, 0, nums.length - 2), getRob(nums, 1, nums.length - 1));
    }

    public static int getRob(int[] nums, int start, int end) {
        int[] res = new int[nums.length];
        for (int i = start; i <= end; i++) {
            res[i] = nums[i];
            if (i > 0) res[i] = Math.max(res[i - 1], res[i]);
            if (i > 1) res[i] = Math.max(nums[i] + res[i - 2], res[i]);
        }
        return res[end];
    }
}
